package com.heima.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.heima.common.PageResult;
import com.heima.mapper.BusinessTypeMapper;
import com.heima.mapper.NodeMapper;
import com.heima.mapper.RegionMapper;
import com.heima.pojo.Node;
import com.heima.pojo.Region;
import com.heima.pojo.VmType;
import com.heima.pojo.dto.AddNodeDTO;
import com.heima.pojo.dto.NodeDTO;
import com.heima.pojo.dto.NodePageSearchQueryDTO;
import com.heima.pojo.vo.BusinessTypeVO;
import com.heima.pojo.vo.NodePageSearchQueryVO;
import com.heima.pojo.vo.NodeVO;
import com.heima.service.NodeService;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class NodeServiceImpl implements NodeService {
    @Autowired
    NodeMapper nodeMapper;
    @Autowired
    RegionMapper regionMapper;
    @Autowired
    BusinessTypeMapper typeMapper;

    /**
     * 点位搜索
     **/
    @Override
    public PageResult<NodePageSearchQueryVO> pageSearch(NodePageSearchQueryDTO dto) {
        //开启分页
        PageHelper.startPage(dto.getPageIndex(), dto.getPageSize());
        Page<NodePageSearchQueryVO> page = nodeMapper.pageSearch(dto);
        //循环查询数据
        for (NodePageSearchQueryVO vo : page) {
          List<Node> nodeListByRegionId =  nodeMapper.getByRegionId(vo.getRegionId());
          vo.getRegion().setNodeCount(nodeListByRegionId.size());
          vo.getRegion().setNodeList(nodeListByRegionId);
        }
        //封装返回
        return new PageResult<>(dto.getPageIndex(),dto.getPageSize(),page.getPages(), page.getTotal(), page.getResult());
    }
    /**
     * 接口一：查询商圈列表
     *
     * @return
     **/
    @Override
    public List<BusinessTypeVO> selectType() {
        List<BusinessTypeVO> list =  nodeMapper.selectType();
        return list;
    }
    /**
     * 新增点位
     * **/
    @Override
    public Boolean add(NodeDTO dto) {
        Node node = new Node();
        //拷贝
        BeanUtils.copyProperties(dto,node);
        //添加创建时间，修改时间
        node.setUpdateTime(LocalDateTime.now());
       node.setCreateTim(LocalDateTime.now());
        nodeMapper.add(node);
        return true;
    }
    /**
     * 获取点位详细
     * **/
    @Override
    public List<NodeVO> list(Long nodeId) {
        List<NodeVO> list = nodeMapper.selectNodeId(nodeId);
        for (NodeVO nodeVO : list) {
            //获取type中的数据
            VmType vmType = nodeMapper.selectTypeId(nodeVO.getVmType());
            nodeVO.setType(vmType);
            //获取node中的数据
          Node node =  nodeMapper.selectByNodeId(nodeVO.getNodeId());
           nodeVO.setNode(node);
            //获取region中的数据
           Region region = regionMapper.selevtById(nodeVO.getRegionId());
            nodeVO.setRegion(region);
        }
        return list;
    }
    /**
     * 删除点位
     * **/
    @Override
    public void deleteById(Long nodeId) {
        nodeMapper.deleteById(nodeId);
    }
    /**
     * 修改点位
     * **/
    @Override
    public void updateNode(AddNodeDTO dto, Long nodeId) {
        Node node = new Node();
        BeanUtils.copyProperties(dto,node);
        node.setId(nodeId);
        node.setUpdateTime(LocalDateTime.now());
        nodeMapper.updateNode(node);
    }
}
